
//
//  HJDNickNameViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/6/23.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDNickNameViewController.h"
#import "AFNetworking.h"


@interface HJDNickNameViewController ()
//昵称
@property (nonatomic,strong)  UITextField *nickText ;

@end

@implementation HJDNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor getColorWithHexNumber:@"CFCFCF"];
    self.title = @"昵称";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor getColorWithHexNumber:@"848484"]}];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor getColorWithHexNumber:@"848484"] forState:UIControlStateNormal];
    [leftBtn sizeToFit];
    [leftBtn addTarget:self action:@selector(backNickNameClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor getColorWithHexNumber:@"5CC6AB"] forState:UIControlStateNormal];
    [rightBtn sizeToFit];
    [rightBtn addTarget:self action:@selector(saveNickNameClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self setUpText];
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    

    
}

//隐藏键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap
{
    
    [self.view endEditing:YES];
    
}

-(void)backNickNameClick{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setUpText{
    
    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    NSString *changeN = [de objectForKey:@"changNick"];
    
    self.nickText = [[UITextField alloc]initWithFrame:CGRectMake(0, HJDVerticalMargin + HJDNavigHeight, HJDWidth, 50)];
    self.nickText.backgroundColor = [UIColor whiteColor];
    self.nickText.text = changeN;

    self.nickText.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.nickText];
}


-(void)saveNickNameClick{
    
    
    if (![_nickText.text isEqualToString:@""] ) {

        [self requestNickName];
    }else{
        [MBProgressHUD showError:@"昵称不能为空"];
    }
    
}

-(void)requestNickName{
    
    NSString *url = @"/app/modifyUser.do";
    NSString *path = [Hurl stringByAppendingString:url];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
    NSString *phone = [defa objectForKey:@"phonenum"];
    NSString *pregnancy = [defa objectForKey:@"pregnanc"];
    NSData *qqDa = [[NSUserDefaults standardUserDefaults]valueForKey:@"qqD"];
    NSString *qq = [NSKeyedUnarchiver unarchiveObjectWithData:qqDa];
    
    NSData *nameDa = [[NSUserDefaults standardUserDefaults]valueForKey:@"nameD"];
    NSString *name = [NSKeyedUnarchiver unarchiveObjectWithData:nameDa];
    
    NSData *dueDa = [[NSUserDefaults standardUserDefaults]valueForKey:@"due"];
    NSString *dueDate = [NSKeyedUnarchiver unarchiveObjectWithData:dueDa];

        params[@"phone"] = phone;
        params[@"nickName"] = self.nickText.text;
        params[@"name"] = name;
        params[@"qq"] = qq;
        params[@"pregnancy"] = pregnancy;
        params[@"dueDate"] = dueDate;
    [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"res%@",responseObject);
        NSUserDefaults *defalt = [NSUserDefaults standardUserDefaults];
                [defalt setObject:self.nickText.text forKey:@"changNick"];
                [defalt synchronize];
        [self.navigationController popViewControllerAnimated:YES];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"服务器升级，请稍后再试"];
    }];
    
}

@end
